<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    /**
     * Constructor de la clase makinita.
     * @param array $entrada
     * @param array $reglas
     * @return boolean $error
     */

     
class sfThSchema extends sfTableWidget implements arrayaccess, Iterator, Countable  {
    
    protected $ths = array();

    
    /**
     * Constructor.
     *
     * Available options:
     *
     *  * id_format:       The format for the generated HTML id attributes (%s by default)
     *  * is_hidden:       true if the form widget must be hidden, false otherwise (false by default)
     *  * default:         The default value to use when rendering the widget
     *  * title:           The title to use when the widget is rendered
     *  * link:            The link of the widget
     *  * defaultSort:     The sort column at start
     *  * defaultSortDirection: The sort column at start
     * 
     * @param array $options     An array of options
     * @param array $attributes  An array of default HTML attributes
     *
     * @see sfWidget
     */
    public function __construct($options = array(), $attributes = array())
    {
      $this->addOption('defaultSort', null);     
      $this->addOption('defaultSortDirection', 'asc');           
      parent::__construct($options, $attributes);
      
      $this->ths = array();

    }    
    
    
    public function offsetSet($offset, $th) {
        if (is_null($offset)) {
            $this->ths[] = $th;
        } else {
            $this->ths[$offset] = $th;
        }
        $th->setParent($this);        
    }
    public function offsetExists($offset) {
        return isset($this->ths[$offset]);
    }
    public function offsetUnset($offset) {
        unset($this->ths[$offset]);
    }
    public function offsetGet($offset) {
        return isset($this->ths[$offset]) ? $this->ths[$offset] : null;
    }
    
    /**
   * Returns the number of form fields (implements the Countable interface).
   *
   * @return integer The number of embedded form fields
   */
    public function count()
    {
      return count($this->ths);
    }    
    
    /**
     * Resets the field names array to the beginning (implements the Iterator interface).
     */
    public function rewind()
    {
      reset($this->ths);
    }

    /**
     * Gets the key associated with the current form field (implements the Iterator interface).
     *
     * @return string The key
     */
    public function key()
    {
      return key($this->ths);
    }

    /**
     * Returns the current form field (implements the Iterator interface).
     *
     * @return mixed The escaped value
     */
    public function current()
    {
      return current($this->ths);
    }

    /**
     * Moves to the next form field (implements the Iterator interface).
     */
    public function next()
    {
      next($this->ths);
    }

    /**
     * Returns true if the current form field is valid (implements the Iterator interface).
     *
     * @return boolean The validity of the current element; true if it is valid
     */
    public function valid()
    {
      return key($this->ths) !== null;
    }

    
    public function setDefaultSort($defaultSort) {
        $this->setOption('defaultSort',$defaultSort);
        return $this;
    }

    public function getDefaultSort() {
        return $this->getOption('defaultSort');
    }

    
    public function setDefaultSortDirection($defaultSortDirection) {
        $this->setOption('defaultSortDirection', $defaultSortDirection);
        return $this;
    }

    public function getDefaultSortDirection() {
        return $this->getOption('defaultSortDirection');
    }
    
    public function render($content = '') {
        
        $content = '';
        
        foreach ($this->ths as $th)
        {
            $content .= chr(10).$th->render(); 
        }
        
//        if(($link = $this->getOption('link')) != '')
//            $result = $this->renderContentTag('a',$this->renderContentTag('tr',                             
//                                    $content,$this->getAttributes()),array('href'=>$this->getOption('link')));        
//        else
            
        $result = $this->renderContentTag('tr', $content, $this->getAttributes());
        
        return $result;
                
    }
    
    
    
    
    
    
    
}


?>
